from config.all_paths import config_variable_path
from config.global_variable import YSDS
from utils.file_handling.txt_handling import extract_txt


def YSDSDataProcessing(data, res):
    """
    互联网医院医师大赛数据统计
    """
    # 获取绑定的医生条数
    # 需要过滤的文章id
    filterId = [1,2,3,4,5,6,7,8,9,10,11,12,684,685,686,687]
    if data.get('case_name') == '今日新增参赛作品相关信息':
        # 先获取所有的数据
        allData = [item for item in res.json()['data']['list'] if item['id'] not in filterId]
        # 参赛的作品数量
        YSDS['todayData']['numberEntries'] = str(len(allData))
        # 参赛的人数，通过获取allData中的数据手机字段，去重
        YSDS['todayData']['numberParticipants'] = len(list(set([entry['phone'] for entry in allData])))
        # 未初审的作品数量
        YSDS['todayData']['unreviewedEntries'] = sum(1 for entry in allData if entry['firstCheckStatus'] == 0)
        # 初审拒绝的作品数量
        YSDS['todayData']['entriesNotPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 2)
        # 初审通过的作品的数量
        YSDS['todayData']['entriesPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 1)
        # 未复审的作品数量
        YSDS['todayData']['unreviewedForRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 0 and entry['firstCheckStatus'] == 1)
        # 复审拒绝的作品数量
        YSDS['todayData']['entriesNotPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 2 and entry['firstCheckStatus'] == 1)
        # 复审通过的作品的数量
        YSDS['todayData']['entriesPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1)
        # 未分配的作品数量
        YSDS['todayData']['unallocated'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is None)
        # 已分配的作品数量
        YSDS['todayData']['assigned'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 未评分的作品数量
        YSDS['todayData']['re-reviewedButNotScored'] = sum(
            1 for entry in allData if entry['score'] is None and entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 评分大于等于80的作品数量
        YSDS['todayData']['score≥80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 评分小于80
        YSDS['todayData']['score<80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) < 80 and entry['secondCheckStatus'] == 1)
        # 邀请中的作品数量
        YSDS['todayData']['invitationPending'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 已邀请
        YSDS['todayData']['invited'] = sum(
            1 for entry in allData if entry['authorizeStatus'] == 1 and entry['creatorCheck'] == 1)
    if data.get('case_name') == '昨日参赛作品相关信息':
        # 先获取所有的数据
        allData = [item for item in res.json()['data']['list'] if item['id'] not in filterId]
        # 参赛的作品数量
        YSDS['yesterdayData']['numberEntries'] = str(len(allData))
        # 参赛的人数，通过获取allData中的数据手机字段，去重
        YSDS['yesterdayData']['numberParticipants'] = len(list(set([entry['phone'] for entry in allData])))
        # 未初审的作品数量
        YSDS['yesterdayData']['unreviewedEntries'] = sum(1 for entry in allData if entry['firstCheckStatus'] == 0)
        # 初审拒绝的作品数量
        YSDS['yesterdayData']['entriesNotPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 2)
        # 初审通过的作品的数量
        YSDS['yesterdayData']['entriesPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 1)
        # 未复审的作品数量
        YSDS['yesterdayData']['unreviewedForRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 0 and entry['firstCheckStatus'] == 1)
        # 复审拒绝的作品数量
        YSDS['yesterdayData']['entriesNotPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 2 and entry['firstCheckStatus'] == 1)
        # 复审通过的作品的数量
        YSDS['yesterdayData']['entriesPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1)
        # 未分配的作品数量
        YSDS['yesterdayData']['unallocated'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is None)
        # 已分配的作品数量
        YSDS['yesterdayData']['assigned'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 未评分的作品数量
        YSDS['yesterdayData']['re-reviewedButNotScored'] = sum(
            1 for entry in allData if entry['score'] is None and entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 评分大于等于80的作品数量
        YSDS['yesterdayData']['score≥80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 评分小于80
        YSDS['yesterdayData']['score<80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) < 80 and entry['secondCheckStatus'] == 1)
        # 邀请中的作品数量
        YSDS['yesterdayData']['invitationPending'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 已邀请
        YSDS['yesterdayData']['invited'] = sum(
            1 for entry in allData if entry['authorizeStatus'] == 1 and entry['creatorCheck'] == 1)
    if data.get('case_name') == '昨日及之前参赛作品相关信息':
        # 先获取所有的数据
        allData = [item for item in res.json()['data']['list'] if item['id'] not in filterId]
        # 参赛的作品数量
        YSDS['yesterdayAndBeforeData']['numberEntries'] = str(len(allData))
        # 参赛的人数，通过获取allData中的数据手机字段，去重
        YSDS['yesterdayAndBeforeData']['numberParticipants'] = len(list(set([entry['phone'] for entry in allData])))
        # 未初审的作品数量
        YSDS['yesterdayAndBeforeData']['unreviewedEntries'] = sum(1 for entry in allData if entry['firstCheckStatus'] == 0)
        # 初审拒绝的作品数量
        YSDS['yesterdayAndBeforeData']['entriesNotPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 2)
        # 初审通过的作品的数量
        YSDS['yesterdayAndBeforeData']['entriesPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 1)
        # 未复审的作品数量
        YSDS['yesterdayAndBeforeData']['unreviewedForRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 0 and entry['firstCheckStatus'] == 1)
        # 复审拒绝的作品数量
        YSDS['yesterdayAndBeforeData']['entriesNotPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 2 and entry['firstCheckStatus'] == 1)
        # 复审通过的作品的数量
        YSDS['yesterdayAndBeforeData']['entriesPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1)
        # 未分配的作品数量
        YSDS['yesterdayAndBeforeData']['unallocated'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is None)
        # 已分配的作品数量
        YSDS['yesterdayAndBeforeData']['assigned'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 未评分的作品数量
        YSDS['yesterdayAndBeforeData']['re-reviewedButNotScored'] = sum(
            1 for entry in allData if entry['score'] is None and entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 评分大于等于80的作品数量
        YSDS['yesterdayAndBeforeData']['score≥80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 评分小于80
        YSDS['yesterdayAndBeforeData']['score<80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) < 80 and entry['secondCheckStatus'] == 1)
        # 邀请中的作品数量
        YSDS['yesterdayAndBeforeData']['invitationPending'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 已邀请
        YSDS['yesterdayAndBeforeData']['invited'] = sum(
            1 for entry in allData if entry['authorizeStatus'] == 1 and entry['creatorCheck'] == 1)
    if data.get('case_name') == '所有参赛作品相关信息':
        # 先获取所有的数据
        allData = [item for item in res.json()['data']['list'] if item['id'] not in filterId]
        # 参赛的作品数量
        YSDS['allData']['numberEntries'] = str(len(allData))
        # 参赛的人数，通过获取allData中的数据手机字段，去重
        YSDS['allData']['numberParticipants'] = len(list(set([entry['phone'] for entry in allData])))
        # 未初审的作品数量
        YSDS['allData']['unreviewedEntries'] = sum(1 for entry in allData if entry['firstCheckStatus'] == 0)
        # 初审拒绝的作品数量
        YSDS['allData']['entriesNotPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 2)
        # 初审通过的作品的数量
        YSDS['allData']['entriesPassingPreliminaryReview'] = sum(
            1 for entry in allData if entry['firstCheckStatus'] == 1)
        # 未复审的作品数量
        YSDS['allData']['unreviewedForRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 0 and entry['firstCheckStatus'] == 1)
        # 复审拒绝的作品数量
        YSDS['allData']['entriesNotPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 2 and entry['firstCheckStatus'] == 1)
        # 复审通过的作品的数量
        YSDS['allData']['entriesPassingRe-review'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1)
        # 未分配的作品数量
        YSDS['allData']['unallocated'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is None)
        # 已分配的作品数量
        YSDS['allData']['assigned'] = sum(
            1 for entry in allData if entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 未评分的作品数量
        YSDS['allData']['re-reviewedButNotScored'] = sum(
            1 for entry in allData if entry['score'] is None and entry['secondCheckStatus'] == 1 and entry['firstCheckStatus'] == 1
            and entry['raterName'] is not None)
        # 评分大于等于80的作品数量
        YSDS['allData']['score≥80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 评分小于80
        YSDS['allData']['score<80'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) < 80 and entry['secondCheckStatus'] == 1)
        # 邀请中的作品数量
        YSDS['allData']['invitationPending'] = sum(
            1 for entry in allData if
            entry['score'] is not None and int(entry['score']) >= 80 and entry['secondCheckStatus'] == 1)
        # 已邀请
        YSDS['allData']['invited'] = sum(
            1 for entry in allData if entry['authorizeStatus'] == 1 and entry['creatorCheck'] == 1)

    extract_txt(config_variable_path, str(YSDS))
